package com.xq.tmall.agu.openapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xq.tmall.agu.core.agent.WebAgent;
import com.xq.tmall.agu.ferign.StockSpider;
import com.xq.tmall.agu.task.EasyCrawl;
import com.xq.tmall.entity.WebPage;
import com.xq.tmall.util.JsoupUitl;
import com.xq.tmall.util.TradingDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用挖地免的接口
 *
 * @author chenguoxiang
 * @create 2018-10-30 9:31
 **/
//@RefreshScope
@Slf4j
@Component("TushareSpider")
public class TushareApi {

    @Value("${tushare.token}")
    private String tuToken="";
    @Resource
    private RestTemplate restTemplate=new RestTemplate();
    final String api = "http://api.tushare.pro";

    Map<String, String> cookies = getXQCookies();

    /**
     * 格式化成tushare API所需格式
     *
     * @param code
     * @return
     */
    public static String formatCode(String code) {
        //5开头，沪市基金或权证 60开头上证
        if (code.matches("^60.*|^68.*|^5.*")) {
            return String.format("%s.SH", code);
        }
        //1开头的，是深市基金 00开头是深圳
        else if (code.matches("^1.*|^00.*|^300...")) {
            return String.format("%s.SZ", code);
        }
        return null;
    }

    /**
     * post 方式提交
     *
     * @param params
     * @return
     */
    String post(JSONObject params) {
        HttpHeaders headers = new HttpHeaders();
        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
        headers.setContentType(MediaType.APPLICATION_JSON);
        params.put("token", tuToken);
        HttpEntity<String> formEntity = new HttpEntity<String>(params.toString(), headers);
        String result = restTemplate.postForObject(api, formEntity, String.class);
        return result;
    }


    String useAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36";
    final static String ProxyUser = "18273179226";
    final static String ProxyPass = "W8Cv3yBU";

    // 代理IP、端口号
    final static String ProxyHost = "tunnel-41.91http.cc";
    final static Integer ProxyPort = 29661;

    String apiUrl = "https://xueqiu.com/";


    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ProxyHost, ProxyPort));

    /**
     * 返回已上市的A股代码
     *
     * @return TS代码, 股票代码, 股票名称, 所在地域, 所属行业, 股票全称, 市场类型 （主板/中小板/创业板）,上市状态： L上市 D退市 P暂停上市,上市日期
     */
    public List<JSONArray> getStockCodeList() {
        Authenticator.setDefault(new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(ProxyUser, ProxyPass.toCharArray());
            }
        });

        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

        List<JSONArray> arrayList = new ArrayList<>();

        for(int i  = 1 ; i<=56;i++){
            int page = i;
            String result = new EasyCrawl<String>()
                    .webAgent(WebAgent.defaultAgent().referer("https://xueqiu.com/").cookie(cookies).url("https://stock.xueqiu.com/v5/stock/screener/quote/list.json?page="+page+"&size=90&order=desc&order_by=percent&market=CN&type=sh_sz").proxy(proxy))
                    .analyze(r -> r.getResult().getBody()).execute();
            System.out.println(result);
            JSONObject jsonObject = JSON.parseObject(result);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray jsonArray = data.getJSONArray("list");
            arrayList.add(jsonArray);
        }
        System.out.println("最后结果:" + arrayList);
        //String result = "{\"data\":{\"count\":5000,\"list\":[{\"symbol\":\"SZ301678\",\"net_profit_cagr\":18.249299557404598,\"north_net_inflow\":null,\"ps\":11.3405,\"type\":11,\"percent\":229.06,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":7.935,\"float_shares\":45512773,\"current\":42.12,\"amplitude\":63.91,\"pcf\":43.342,\"current_year_percent\":229.06,\"float_market_capital\":1.916997999E9,\"north_net_inflow_time\":null,\"market_capital\":1.009007627E10,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":null,\"total_percent\":-15.76,\"percent5m\":-1.61,\"income_cagr\":15.371862995393304,\"amount\":1.61161155158E9,\"chg\":29.32,\"issue_date_ts\":1750348800000,\"eps\":0.77,\"main_net_inflows\":0.0,\"volume\":35644663,\"volume_ratio\":null,\"pb\":null,\"followers\":1217,\"turnover_rate\":78.32,\"mapping_quote_current\":null,\"first_percent\":229.06,\"name\":\"N新恒汇\",\"pe_ttm\":54.604,\"dual_counter_mapping_symbol\":null,\"total_shares\":239555467,\"limitup_days\":0},{\"symbol\":\"SH603400\",\"net_profit_cagr\":21.487506200484184,\"north_net_inflow\":null,\"ps\":4.1192,\"type\":11,\"percent\":170.98,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":6.914,\"float_shares\":19597624,\"current\":53.87,\"amplitude\":80.78,\"pcf\":30.7866,\"current_year_percent\":170.98,\"float_market_capital\":1.055724005E9,\"north_net_inflow_time\":null,\"market_capital\":5.387E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":null,\"total_percent\":-11.69,\"percent5m\":0.32,\"income_cagr\":16.225246740019415,\"amount\":1.003135777E9,\"chg\":33.99,\"issue_date_ts\":1750348800000,\"eps\":1.6,\"main_net_inflows\":0.0,\"volume\":15938557,\"volume_ratio\":null,\"pb\":4.404,\"followers\":794,\"turnover_rate\":81.33,\"mapping_quote_current\":null,\"first_percent\":170.98,\"name\":\"N华之杰\",\"pe_ttm\":33.639,\"dual_counter_mapping_symbol\":null,\"total_shares\":100000000,\"limitup_days\":0},{\"symbol\":\"SZ301038\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":4.5712,\"type\":11,\"percent\":20.03,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":4.455,\"float_shares\":223080000,\"current\":17.26,\"amplitude\":19.89,\"pcf\":56.1138,\"current_year_percent\":19.78,\"float_market_capital\":3.8503608E9,\"north_net_inflow_time\":null,\"market_capital\":3.8503608E9,\"dividend_yield\":0.411,\"lot_size\":100,\"roe_ttm\":4.308686172168376,\"total_percent\":9.93,\"percent5m\":0.0,\"income_cagr\":-6.676213437619838,\"amount\":3.8129385993E8,\"chg\":2.88,\"issue_date_ts\":1628006400000,\"eps\":0.16,\"main_net_inflows\":2.9856104E7,\"volume\":23807821,\"volume_ratio\":3.29,\"pb\":4.455,\"followers\":7825,\"turnover_rate\":10.67,\"mapping_quote_current\":null,\"first_percent\":257.34,\"name\":\"深水规院\",\"pe_ttm\":105.632,\"dual_counter_mapping_symbol\":null,\"total_shares\":223080000,\"limitup_days\":1},{\"symbol\":\"SZ300429\",\"net_profit_cagr\":null,\"north_net_inflow\":0.0,\"ps\":8.2566,\"type\":11,\"percent\":20.02,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":4.395,\"float_shares\":398644716,\"current\":14.27,\"amplitude\":12.87,\"pcf\":168.6186,\"current_year_percent\":19.02,\"float_market_capital\":5.688660097E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":7.653010304E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-10.734576129699931,\"total_percent\":312.74,\"percent5m\":0.0,\"income_cagr\":1.6603937205733388,\"amount\":1.67923230316E9,\"chg\":2.38,\"issue_date_ts\":1427126400000,\"eps\":-0.37,\"main_net_inflows\":1.74289914E8,\"volume\":120027414,\"volume_ratio\":9.78,\"pb\":4.395,\"followers\":39105,\"turnover_rate\":30.11,\"mapping_quote_current\":null,\"first_percent\":43.99,\"name\":\"强力新材\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":536300652,\"limitup_days\":1},{\"symbol\":\"SZ300480\",\"net_profit_cagr\":null,\"north_net_inflow\":0.0,\"ps\":9.4746,\"type\":11,\"percent\":20.02,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":4.242,\"float_shares\":236648580,\"current\":15.59,\"amplitude\":16.4,\"pcf\":null,\"current_year_percent\":20.85,\"float_market_capital\":3.689351362E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":5.500613495E9,\"dividend_yield\":0.321,\"lot_size\":100,\"roe_ttm\":-7.951523249289745,\"total_percent\":473.06,\"percent5m\":0.0,\"income_cagr\":-0.6876014064934655,\"amount\":6.7479914481E8,\"chg\":2.6,\"issue_date_ts\":1435766400000,\"eps\":-0.31,\"main_net_inflows\":5112350.0,\"volume\":45475357,\"volume_ratio\":12.69,\"pb\":4.242,\"followers\":27860,\"turnover_rate\":19.22,\"mapping_quote_current\":null,\"first_percent\":43.96,\"name\":\"光力科技\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":352829602,\"limitup_days\":1},{\"symbol\":\"SZ300340\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":2.7056,\"type\":11,\"percent\":20.0,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":102.194,\"float_shares\":210464562,\"current\":20.64,\"amplitude\":24.59,\"pcf\":24.2529,\"current_year_percent\":116.58,\"float_market_capital\":4.34398856E9,\"north_net_inflow_time\":null,\"market_capital\":5.694439054E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-115.26548746718962,\"total_percent\":24.41,\"percent5m\":0.0,\"income_cagr\":-19.908662250041388,\"amount\":2.14765446088E9,\"chg\":3.44,\"issue_date_ts\":1343232000000,\"eps\":-0.64,\"main_net_inflows\":8.6060518E7,\"volume\":111240912,\"volume_ratio\":1.55,\"pb\":102.381,\"followers\":44632,\"turnover_rate\":52.85,\"mapping_quote_current\":null,\"first_percent\":52.4,\"name\":\"科恒股份\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":275893365,\"limitup_days\":1},{\"symbol\":\"SZ301292\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":1.3495,\"type\":11,\"percent\":20.0,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.844,\"float_shares\":85119956,\"current\":23.52,\"amplitude\":11.38,\"pcf\":82.3233,\"current_year_percent\":79.82,\"float_market_capital\":2.002021365E9,\"north_net_inflow_time\":null,\"market_capital\":5.240156699E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-7.3316987256693,\"total_percent\":-20.78,\"percent5m\":0.0,\"income_cagr\":16.134494045761105,\"amount\":6.8006643962E8,\"chg\":3.92,\"issue_date_ts\":1688659200000,\"eps\":-0.97,\"main_net_inflows\":-3.545558419999999E7,\"volume\":29829945,\"volume_ratio\":2.93,\"pb\":1.844,\"followers\":3171,\"turnover_rate\":35.04,\"mapping_quote_current\":null,\"first_percent\":48.57,\"name\":\"海科新源\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":222795778,\"limitup_days\":2},{\"symbol\":\"SZ300589\",\"net_profit_cagr\":null,\"north_net_inflow\":0.0,\"ps\":3.7517,\"type\":11,\"percent\":20.0,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":6.934,\"float_shares\":231607014,\"current\":15.12,\"amplitude\":22.38,\"pcf\":null,\"current_year_percent\":21.45,\"float_market_capital\":3.501898052E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":5.7103401E9,\"dividend_yield\":0.198,\"lot_size\":100,\"roe_ttm\":-0.6319707399045723,\"total_percent\":624.9,\"percent5m\":0.0,\"income_cagr\":2.147584244701539,\"amount\":9.9619984625E8,\"chg\":2.52,\"issue_date_ts\":1484236800000,\"eps\":-0.01,\"main_net_inflows\":1.53959939E8,\"volume\":70421791,\"volume_ratio\":4.19,\"pb\":6.935,\"followers\":17546,\"turnover_rate\":30.41,\"mapping_quote_current\":null,\"first_percent\":44.01,\"name\":\"江龙船艇\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":377667996,\"limitup_days\":1},{\"symbol\":\"SZ300410\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":3.9951,\"type\":11,\"percent\":19.97,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":12.036,\"float_shares\":366917328,\"current\":7.45,\"amplitude\":20.29,\"pcf\":35.0954,\"current_year_percent\":38.48,\"float_market_capital\":2.733534094E9,\"north_net_inflow_time\":null,\"market_capital\":2.735005238E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-61.85998135157542,\"total_percent\":181.87,\"percent5m\":0.0,\"income_cagr\":-14.803429654051104,\"amount\":2.4265776277E8,\"chg\":1.24,\"issue_date_ts\":1419955200000,\"eps\":-0.55,\"main_net_inflows\":-6.8691167E7,\"volume\":33601912,\"volume_ratio\":2.94,\"pb\":12.036,\"followers\":18424,\"turnover_rate\":9.16,\"mapping_quote_current\":null,\"first_percent\":44.02,\"name\":\"正业科技\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":367114797,\"limitup_days\":1},{\"symbol\":\"SZ300485\",\"net_profit_cagr\":-58.68564600056156,\"north_net_inflow\":null,\"ps\":16.8078,\"type\":11,\"percent\":19.97,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.977,\"float_shares\":273817182,\"current\":13.94,\"amplitude\":17.9,\"pcf\":64.2265,\"current_year_percent\":98.86,\"float_market_capital\":3.817011517E9,\"north_net_inflow_time\":null,\"market_capital\":6.714429616E9,\"dividend_yield\":0.158,\"lot_size\":100,\"roe_ttm\":4.869697292293875,\"total_percent\":17.34,\"percent5m\":0.0,\"income_cagr\":-21.757057610120267,\"amount\":7.2058049047E8,\"chg\":2.32,\"issue_date_ts\":1435248000000,\"eps\":0.34,\"main_net_inflows\":1.65567386E8,\"volume\":54364729,\"volume_ratio\":0.92,\"pb\":1.977,\"followers\":27929,\"turnover_rate\":19.85,\"mapping_quote_current\":null,\"first_percent\":43.99,\"name\":\"赛升药业\",\"pe_ttm\":41.515,\"dual_counter_mapping_symbol\":null,\"total_shares\":481666400,\"limitup_days\":1},{\"symbol\":\"SZ301150\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":1.0071,\"type\":11,\"percent\":18.44,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.454,\"float_shares\":117961947,\"current\":21.9,\"amplitude\":20.66,\"pcf\":21.5166,\"current_year_percent\":31.53,\"float_market_capital\":2.583366639E9,\"north_net_inflow_time\":null,\"market_capital\":5.107812927E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-1.0511933286079376,\"total_percent\":-35.32,\"percent5m\":0.69,\"income_cagr\":28.953652964443187,\"amount\":6.5285870875E8,\"chg\":3.41,\"issue_date_ts\":1650470400000,\"eps\":-0.16,\"main_net_inflows\":9.35378814E7,\"volume\":31569863,\"volume_ratio\":5.91,\"pb\":1.454,\"followers\":4227,\"turnover_rate\":26.76,\"mapping_quote_current\":null,\"first_percent\":-24.62,\"name\":\"中一科技\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":233233467,\"limitup_days\":0},{\"symbol\":\"SH688117\",\"net_profit_cagr\":30.4339001857582,\"north_net_inflow\":null,\"ps\":10.4787,\"type\":82,\"percent\":15.01,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":5.817,\"float_shares\":157385978,\"current\":35.71,\"amplitude\":20.45,\"pcf\":255.7237,\"current_year_percent\":95.32,\"float_market_capital\":5.620253274E9,\"north_net_inflow_time\":null,\"market_capital\":5.620253274E9,\"dividend_yield\":0.28,\"lot_size\":1,\"roe_ttm\":8.661872360016948,\"total_percent\":-11.26,\"percent5m\":-0.42,\"income_cagr\":26.86128500894831,\"amount\":8.79991367E8,\"chg\":4.66,\"issue_date_ts\":1622649600000,\"eps\":0.51,\"main_net_inflows\":6.978734384E7,\"volume\":25988727,\"volume_ratio\":2.31,\"pb\":5.817,\"followers\":11569,\"turnover_rate\":16.51,\"mapping_quote_current\":null,\"first_percent\":265.98,\"name\":\"圣诺生物\",\"pe_ttm\":69.669,\"dual_counter_mapping_symbol\":null,\"total_shares\":157385978,\"limitup_days\":0},{\"symbol\":\"SZ300462\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":3.5976,\"type\":11,\"percent\":13.22,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.471,\"float_shares\":136734413,\"current\":11.56,\"amplitude\":21.74,\"pcf\":null,\"current_year_percent\":29.6,\"float_market_capital\":1.580649814E9,\"north_net_inflow_time\":null,\"market_capital\":2.094925603E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":0.2355349109535935,\"total_percent\":27.22,\"percent5m\":-0.26,\"income_cagr\":-9.91079231747165,\"amount\":5.033641104E8,\"chg\":1.35,\"issue_date_ts\":1432656000000,\"eps\":0.02,\"main_net_inflows\":-4.2049383E7,\"volume\":43197768,\"volume_ratio\":3.8,\"pb\":1.471,\"followers\":20249,\"turnover_rate\":31.59,\"mapping_quote_current\":null,\"first_percent\":44.0,\"name\":\"华铭智能\",\"pe_ttm\":617.36,\"dual_counter_mapping_symbol\":null,\"total_shares\":181221938,\"limitup_days\":0},{\"symbol\":\"SZ300269\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":5.3991,\"type\":11,\"percent\":13.21,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":39.203,\"float_shares\":525675103,\"current\":6.17,\"amplitude\":18.72,\"pcf\":31.655,\"current_year_percent\":55.03,\"float_market_capital\":3.243415386E9,\"north_net_inflow_time\":null,\"market_capital\":3.3880323E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":5.017479626331936,\"total_percent\":30.49,\"percent5m\":3.01,\"income_cagr\":-26.805791914492605,\"amount\":1.23107108576E9,\"chg\":0.72,\"issue_date_ts\":1318348800000,\"eps\":0.01,\"main_net_inflows\":8.138199E7,\"volume\":209380086,\"volume_ratio\":2.33,\"pb\":39.199,\"followers\":20853,\"turnover_rate\":39.83,\"mapping_quote_current\":null,\"first_percent\":12.3,\"name\":\"联建光电\",\"pe_ttm\":818.0,\"dual_counter_mapping_symbol\":null,\"total_shares\":549113825,\"limitup_days\":0},{\"symbol\":\"SZ300950\",\"net_profit_cagr\":-11.651228032850192,\"north_net_inflow\":null,\"ps\":7.1436,\"type\":11,\"percent\":12.71,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":4.226,\"float_shares\":90374775,\"current\":21.2,\"amplitude\":15.74,\"pcf\":42.6151,\"current_year_percent\":32.91,\"float_market_capital\":1.91594523E9,\"north_net_inflow_time\":null,\"market_capital\":3.232576E9,\"dividend_yield\":0.929,\"lot_size\":100,\"roe_ttm\":9.415438694930081,\"total_percent\":-45.18,\"percent5m\":0.19,\"income_cagr\":2.684141413998087,\"amount\":3.5776673864E8,\"chg\":2.39,\"issue_date_ts\":1614700800000,\"eps\":0.46,\"main_net_inflows\":4.3245058E7,\"volume\":17499082,\"volume_ratio\":2.07,\"pb\":4.171,\"followers\":9394,\"turnover_rate\":19.36,\"mapping_quote_current\":null,\"first_percent\":629.96,\"name\":\"德固特\",\"pe_ttm\":46.34,\"dual_counter_mapping_symbol\":null,\"total_shares\":152480000,\"limitup_days\":0},{\"symbol\":\"SZ300655\",\"net_profit_cagr\":74.35853529955538,\"north_net_inflow\":0.0,\"ps\":6.816,\"type\":11,\"percent\":11.25,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":4.102,\"float_shares\":997570521,\"current\":9.49,\"amplitude\":15.83,\"pcf\":34.5928,\"current_year_percent\":1.92,\"float_market_capital\":9.466944244E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":1.0055338176E10,\"dividend_yield\":0.474,\"lot_size\":100,\"roe_ttm\":-4.839538801679979,\"total_percent\":937.84,\"percent5m\":0.21,\"income_cagr\":-2.666517033215776,\"amount\":1.52720887934E9,\"chg\":0.96,\"issue_date_ts\":1495468800000,\"eps\":-0.12,\"main_net_inflows\":3.58931811E8,\"volume\":163616487,\"volume_ratio\":9.78,\"pb\":4.102,\"followers\":59289,\"turnover_rate\":16.4,\"mapping_quote_current\":null,\"first_percent\":43.93,\"name\":\"晶瑞电材\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":1059571989,\"limitup_days\":0},{\"symbol\":\"SZ300483\",\"net_profit_cagr\":174.88366019061442,\"north_net_inflow\":null,\"ps\":2.0292,\"type\":11,\"percent\":10.99,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":2.146,\"float_shares\":268555379,\"current\":14.85,\"amplitude\":18.16,\"pcf\":4.9343,\"current_year_percent\":50.91,\"float_market_capital\":3.988047378E9,\"north_net_inflow_time\":null,\"market_capital\":3.988047378E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-28.716890659868948,\"total_percent\":96.93,\"percent5m\":-0.27,\"income_cagr\":24.61501664628143,\"amount\":1.07264991856E9,\"chg\":1.47,\"issue_date_ts\":1435593600000,\"eps\":-2.5,\"main_net_inflows\":1.82465206E8,\"volume\":74351569,\"volume_ratio\":1.7,\"pb\":2.145,\"followers\":21168,\"turnover_rate\":27.69,\"mapping_quote_current\":null,\"first_percent\":43.99,\"name\":\"首华燃气\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":268555379,\"limitup_days\":0},{\"symbol\":\"SZ300120\",\"net_profit_cagr\":-38.07062072216787,\"north_net_inflow\":null,\"ps\":1.696,\"type\":11,\"percent\":10.84,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.745,\"float_shares\":527447002,\"current\":9.1,\"amplitude\":12.79,\"pcf\":614.9618,\"current_year_percent\":-2.15,\"float_market_capital\":4.799767718E9,\"north_net_inflow_time\":null,\"market_capital\":5.226984754E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":0.09317031163797043,\"total_percent\":0.57,\"percent5m\":0.55,\"income_cagr\":-2.544383901118652,\"amount\":7.9914294961E8,\"chg\":0.89,\"issue_date_ts\":1284652800000,\"eps\":0.0,\"main_net_inflows\":1.42875892E8,\"volume\":89888295,\"volume_ratio\":4.25,\"pb\":1.745,\"followers\":31056,\"turnover_rate\":17.04,\"mapping_quote_current\":null,\"first_percent\":27.19,\"name\":\"经纬辉开\",\"pe_ttm\":1878.733,\"dual_counter_mapping_symbol\":null,\"total_shares\":574393929,\"limitup_days\":0},{\"symbol\":\"SZ301079\",\"net_profit_cagr\":-59.89573260061481,\"north_net_inflow\":null,\"ps\":9.8767,\"type\":11,\"percent\":10.19,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":7.245,\"float_shares\":69587774,\"current\":31.9,\"amplitude\":12.78,\"pcf\":238.7322,\"current_year_percent\":109.46,\"float_market_capital\":2.219849991E9,\"north_net_inflow_time\":null,\"market_capital\":3.487887272E9,\"dividend_yield\":0.063,\"lot_size\":100,\"roe_ttm\":1.1606908783181502,\"total_percent\":39.6,\"percent5m\":0.79,\"income_cagr\":6.442550055466412,\"amount\":5.3911350984E8,\"chg\":2.95,\"issue_date_ts\":1634572800000,\"eps\":0.05,\"main_net_inflows\":3.8901168E7,\"volume\":17737978,\"volume_ratio\":1.15,\"pb\":7.245,\"followers\":7323,\"turnover_rate\":25.49,\"mapping_quote_current\":null,\"first_percent\":170.05,\"name\":\"邵阳液压\",\"pe_ttm\":622.509,\"dual_counter_mapping_symbol\":null,\"total_shares\":109338159,\"limitup_days\":0},{\"symbol\":\"SH603616\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":2.0205,\"type\":11,\"percent\":10.12,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":7.355,\"float_shares\":381368000,\"current\":4.46,\"amplitude\":10.12,\"pcf\":157.8522,\"current_year_percent\":27.07,\"float_market_capital\":1.70090128E9,\"north_net_inflow_time\":null,\"market_capital\":1.74527828E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-66.01854019027896,\"total_percent\":-34.03,\"percent5m\":0.0,\"income_cagr\":60.32438744482833,\"amount\":8.8136386E7,\"chg\":0.41,\"issue_date_ts\":1433952000000,\"eps\":-0.55,\"main_net_inflows\":7074227.0,\"volume\":20650100,\"volume_ratio\":1.91,\"pb\":7.355,\"followers\":34899,\"turnover_rate\":5.41,\"mapping_quote_current\":null,\"first_percent\":43.96,\"name\":\"韩建河山\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":391318000,\"limitup_days\":1},{\"symbol\":\"SH600798\",\"net_profit_cagr\":-28.37370686415933,\"north_net_inflow\":null,\"ps\":2.2187,\"type\":11,\"percent\":10.1,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.344,\"float_shares\":1206534201,\"current\":4.36,\"amplitude\":13.89,\"pcf\":9.8201,\"current_year_percent\":43.89,\"float_market_capital\":5.260489116E9,\"north_net_inflow_time\":null,\"market_capital\":5.260489116E9,\"dividend_yield\":0.688,\"lot_size\":100,\"roe_ttm\":0.006640505479639673,\"total_percent\":105.22,\"percent5m\":0.0,\"income_cagr\":0.47599414797068196,\"amount\":7.26235738E8,\"chg\":0.4,\"issue_date_ts\":861724800000,\"eps\":0.0,\"main_net_inflows\":-1.20814747E8,\"volume\":170900491,\"volume_ratio\":0.9,\"pb\":1.344,\"followers\":48015,\"turnover_rate\":14.16,\"mapping_quote_current\":null,\"first_percent\":0.0,\"name\":\"宁波海运\",\"pe_ttm\":20151.728,\"dual_counter_mapping_symbol\":null,\"total_shares\":1206534201,\"limitup_days\":1},{\"symbol\":\"SZ002067\",\"net_profit_cagr\":null,\"north_net_inflow\":null,\"ps\":0.8911,\"type\":11,\"percent\":10.06,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":0.84,\"float_shares\":1119948070,\"current\":3.83,\"amplitude\":3.45,\"pcf\":793.2764,\"current_year_percent\":-3.28,\"float_market_capital\":4.289401108E9,\"north_net_inflow_time\":null,\"market_capital\":4.815068608E9,\"dividend_yield\":0.627,\"lot_size\":100,\"roe_ttm\":0.4595328066353604,\"total_percent\":75.16,\"percent5m\":0.0,\"income_cagr\":-6.133944355817144,\"amount\":4.8075675686E8,\"chg\":0.35,\"issue_date_ts\":1158249600000,\"eps\":0.02,\"main_net_inflows\":-5856065.0,\"volume\":126036835,\"volume_ratio\":5.57,\"pb\":0.84,\"followers\":36302,\"turnover_rate\":11.25,\"mapping_quote_current\":null,\"first_percent\":69.71,\"name\":\"景兴纸业\",\"pe_ttm\":183.515,\"dual_counter_mapping_symbol\":null,\"total_shares\":1257198070,\"limitup_days\":1},{\"symbol\":\"SH600110\",\"net_profit_cagr\":null,\"north_net_inflow\":0.0,\"ps\":1.5852,\"type\":11,\"percent\":10.04,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.557,\"float_shares\":1735180932,\"current\":5.15,\"amplitude\":5.56,\"pcf\":15.9271,\"current_year_percent\":28.75,\"float_market_capital\":8.9361818E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":8.9361818E9,\"dividend_yield\":1.961,\"lot_size\":100,\"roe_ttm\":-5.038475146352367,\"total_percent\":502.41,\"percent5m\":0.0,\"income_cagr\":14.596359144592919,\"amount\":6.73904817E8,\"chg\":0.47,\"issue_date_ts\":876153600000,\"eps\":-0.17,\"main_net_inflows\":2.5681945E7,\"volume\":132291229,\"volume_ratio\":1.11,\"pb\":1.557,\"followers\":69909,\"turnover_rate\":7.62,\"mapping_quote_current\":null,\"first_percent\":0.0,\"name\":\"诺德股份\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":1735180932,\"limitup_days\":4},{\"symbol\":\"SH600773\",\"net_profit_cagr\":-57.91634737085134,\"north_net_inflow\":0.0,\"ps\":5.78,\"type\":11,\"percent\":10.04,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.972,\"float_shares\":951586865,\"current\":9.97,\"amplitude\":10.38,\"pcf\":null,\"current_year_percent\":-7.51,\"float_market_capital\":9.487321044E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":9.487321044E9,\"dividend_yield\":0.1,\"lot_size\":100,\"roe_ttm\":0.6404891074273427,\"total_percent\":307.95,\"percent5m\":0.0,\"income_cagr\":49.92542128720516,\"amount\":3.98283728E8,\"chg\":0.91,\"issue_date_ts\":847382400000,\"eps\":0.03,\"main_net_inflows\":6.9662318E7,\"volume\":41603642,\"volume_ratio\":5.01,\"pb\":1.972,\"followers\":69347,\"turnover_rate\":4.37,\"mapping_quote_current\":null,\"first_percent\":177.99,\"name\":\"西藏城投\",\"pe_ttm\":344.398,\"dual_counter_mapping_symbol\":null,\"total_shares\":951586865,\"limitup_days\":1},{\"symbol\":\"SH603212\",\"net_profit_cagr\":24.552843690169013,\"north_net_inflow\":0.0,\"ps\":1.6838,\"type\":11,\"percent\":10.04,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.761,\"float_shares\":437490636,\"current\":10.63,\"amplitude\":1.45,\"pcf\":12.2076,\"current_year_percent\":4.42,\"float_market_capital\":4.650525461E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":4.650525461E9,\"dividend_yield\":0.677,\"lot_size\":100,\"roe_ttm\":-12.238842498027394,\"total_percent\":-27.92,\"percent5m\":0.0,\"income_cagr\":-19.057329255638166,\"amount\":1.64099395E8,\"chg\":0.97,\"issue_date_ts\":1588176000000,\"eps\":-0.79,\"main_net_inflows\":-5.4834345E7,\"volume\":15475183,\"volume_ratio\":2.29,\"pb\":1.761,\"followers\":28680,\"turnover_rate\":3.54,\"mapping_quote_current\":null,\"first_percent\":43.98,\"name\":\"赛伍技术\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":437490636,\"limitup_days\":1},{\"symbol\":\"SZ002490\",\"net_profit_cagr\":-86.30072948854274,\"north_net_inflow\":0.0,\"ps\":3.8517,\"type\":11,\"percent\":10.03,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":11.253,\"float_shares\":541722000,\"current\":7.02,\"amplitude\":15.05,\"pcf\":13.1002,\"current_year_percent\":152.52,\"float_market_capital\":3.80288844E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":5.600895768E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-44.779863155095605,\"total_percent\":-28.25,\"percent5m\":0.0,\"income_cagr\":-9.626281985872154,\"amount\":1.29156535887E9,\"chg\":0.64,\"issue_date_ts\":1287590400000,\"eps\":-0.32,\"main_net_inflows\":-1.52694931E8,\"volume\":189167262,\"volume_ratio\":1.89,\"pb\":11.254,\"followers\":30624,\"turnover_rate\":34.92,\"mapping_quote_current\":null,\"first_percent\":14.06,\"name\":\"山东墨龙\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":797848400,\"limitup_days\":6},{\"symbol\":\"SZ002125\",\"net_profit_cagr\":-7.99716662685388,\"north_net_inflow\":0.0,\"ps\":3.926,\"type\":11,\"percent\":10.03,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":2.541,\"float_shares\":629481713,\"current\":12.29,\"amplitude\":0.0,\"pcf\":19.0634,\"current_year_percent\":23.92,\"float_market_capital\":7.736330253E9,\"north_net_inflow_time\":1750262400000,\"market_capital\":7.736330253E9,\"dividend_yield\":1.302,\"lot_size\":100,\"roe_ttm\":10.676897543511968,\"total_percent\":186.25,\"percent5m\":0.0,\"income_cagr\":1.8854589233134211,\"amount\":1.7714733489E8,\"chg\":1.12,\"issue_date_ts\":1175529600000,\"eps\":0.5,\"main_net_inflows\":-1.31251055E8,\"volume\":14413941,\"volume_ratio\":0.92,\"pb\":2.541,\"followers\":59562,\"turnover_rate\":2.29,\"mapping_quote_current\":null,\"first_percent\":232.31,\"name\":\"湘潭电化\",\"pe_ttm\":24.656,\"dual_counter_mapping_symbol\":null,\"total_shares\":629481713,\"limitup_days\":2},{\"symbol\":\"SH603155\",\"net_profit_cagr\":-29.42525245687656,\"north_net_inflow\":null,\"ps\":6.4741,\"type\":11,\"percent\":10.02,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":1.98,\"float_shares\":315786800,\"current\":14.39,\"amplitude\":8.94,\"pcf\":35.6613,\"current_year_percent\":10.14,\"float_market_capital\":4.544172052E9,\"north_net_inflow_time\":null,\"market_capital\":4.544172052E9,\"dividend_yield\":4.864,\"lot_size\":100,\"roe_ttm\":4.891604648311042,\"total_percent\":-13.49,\"percent5m\":0.0,\"income_cagr\":-16.951983289211203,\"amount\":6.5498883E7,\"chg\":1.31,\"issue_date_ts\":1598889600000,\"eps\":0.35,\"main_net_inflows\":-1.32418E7,\"volume\":4622867,\"volume_ratio\":2.18,\"pb\":1.98,\"followers\":11579,\"turnover_rate\":1.46,\"mapping_quote_current\":null,\"first_percent\":43.99,\"name\":\"新亚强\",\"pe_ttm\":40.592,\"dual_counter_mapping_symbol\":null,\"total_shares\":315786800,\"limitup_days\":1},{\"symbol\":\"SH603022\",\"net_profit_cagr\":13.398858746723509,\"north_net_inflow\":null,\"ps\":2.1359,\"type\":11,\"percent\":10.02,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":2.304,\"float_shares\":200000000,\"current\":9.33,\"amplitude\":10.38,\"pcf\":21.4526,\"current_year_percent\":28.95,\"float_market_capital\":1.866E9,\"north_net_inflow_time\":null,\"market_capital\":1.866E9,\"dividend_yield\":0.836,\"lot_size\":100,\"roe_ttm\":6.312773251880878,\"total_percent\":17.91,\"percent5m\":0.0,\"income_cagr\":2.9770300453134224,\"amount\":6.3037249E7,\"chg\":0.85,\"issue_date_ts\":1431878400000,\"eps\":0.25,\"main_net_inflows\":-8634404.0,\"volume\":6872900,\"volume_ratio\":1.51,\"pb\":2.304,\"followers\":18653,\"turnover_rate\":3.44,\"mapping_quote_current\":null,\"first_percent\":44.03,\"name\":\"新通联\",\"pe_ttm\":37.184,\"dual_counter_mapping_symbol\":null,\"total_shares\":200000000,\"limitup_days\":1},{\"symbol\":\"SZ000695\",\"net_profit_cagr\":65.24659351884846,\"north_net_inflow\":null,\"ps\":6.1865,\"type\":11,\"percent\":10.02,\"has_follow\":false,\"tick_size\":0.01,\"pb_ttm\":20.075,\"float_shares\":221720364,\"current\":13.28,\"amplitude\":8.7,\"pcf\":null,\"current_year_percent\":28.31,\"float_market_capital\":2.944446434E9,\"north_net_inflow_time\":null,\"market_capital\":2.950119318E9,\"dividend_yield\":null,\"lot_size\":100,\"roe_ttm\":-22.652524038582772,\"total_percent\":160.31,\"percent5m\":0.0,\"income_cagr\":0.46183419894874067,\"amount\":2.28160016E8,\"chg\":1.21,\"issue_date_ts\":856195200000,\"eps\":-0.17,\"main_net_inflows\":-5.4347211E7,\"volume\":17335110,\"volume_ratio\":0.8,\"pb\":20.076,\"followers\":13334,\"turnover_rate\":7.82,\"mapping_quote_current\":null,\"first_percent\":0.0,\"name\":\"滨海能源\",\"pe_ttm\":null,\"dual_counter_mapping_symbol\":null,\"total_shares\":222147539,\"limitup_days\":1}]},\"error_code\":0,\"error_description\":\"\"}\n";
        return arrayList;
    }

    Map<String, String> getXQCookies() {
        String cookieUrl = "https://xueqiu.com/about/contact-us";
        return WebAgent.getCookies(cookieUrl);
    }

    /**
     * 获取上海公司基础信息
     *
     * @return
     */
    public JSONArray getStockShCompany() {
        JSONObject json = new JSONObject();
        //接口名称
        json.put("api_name", "stock_company");
        json.put("params", JSON.parse("{'exchange':'SSE'}"));
        json.put("fields", "ts_code,chairman,manager,secretary,reg_capital,setup_date,province,city,introduction,website,email,office,employees,main_business,business_scope");
        String result = post(json);
        JSONObject data = JSON.parseObject(result);
        JSONArray items = data.getJSONObject("data").getJSONArray("items");
        return items;
    }

    /**
     * 获取深圳公司基础信息
     *
     * @return
     */
    public JSONArray getStockSZCompany() {
        JSONObject json = new JSONObject();
        //接口名称
        json.put("api_name", "stock_company");
        json.put("params", JSON.parse("{'exchange':'SZSE'}"));
        json.put("fields", "ts_code,chairman,manager,secretary,reg_capital,setup_date,province,city,introduction,website,email,office,employees,main_business,business_scope");
        String result = post(json);
        JSONObject data = JSON.parseObject(result);
        JSONArray items = data.getJSONObject("data").getJSONArray("items");
        return items;
    }

    /**
     * 得到前10大持有人
     *
     * @param code
     * @return
     */
    public JSONArray getStockTopHolders(String code) {
        JSONObject json = new JSONObject();
        //接口名称
        json.put("api_name", "top10_holders");
        json.put("params", JSON.parse(String.format("{'ts_code':'%s'}", code)));
        json.put("fields", "ts_code,ann_date,end_date,holder_name,hold_amount,hold_ratio");
        String result = post(json);
        JSONObject datas = JSON.parseObject(result);
        JSONArray items = datas.getJSONObject("data").getJSONArray("items");
        return items;
    }

    /**
     * 股东人数
     *
     * @param code       代码
     * @param start_date 开始日期
     * @param end_date   结束日期
     * @return
     */
    public JSONArray getStockHolderNumber(String code, String start_date, String end_date) {
        JSONObject json = new JSONObject();
        JSONObject params = new JSONObject();
        params.put("ts_code", code);
        params.put("start_date", start_date);
        params.put("end_date", end_date);
        //接口名称
        json.put("api_name", "stk_holdernumber");
        json.put("params", params);
        String result = post(json);
        JSONObject datas = JSON.parseObject(result);
        JSONArray items = datas.getJSONObject("data").getJSONArray("items");
        return items;
    }

    /**
     * 日线行情
     *
     * @param code       代码
     * @param start_date 开始日期
     * @param end_date   结束日期
     * @return
     */
    public JSONArray getStockDailyPrice(String code, String start_date, String end_date) {
        JSONObject json = new JSONObject();
        JSONObject params = new JSONObject();
        params.put("ts_code", code);
        params.put("start_date", start_date);
        params.put("end_date", end_date);
        //接口名称
        json.put("api_name", "daily");
        json.put("params", params);
        String result = post(json);
        JSONObject datas = JSON.parseObject(result);
        JSONArray items = datas.getJSONObject("data").getJSONArray("items");
        return items;
    }

    /**
     * 每日指标基本面
     *
     * @param code       代码
     * @param start_date 开始日期
     * @param end_date   结束日期
     * @return
     */
    public JSONArray getStockDailyBasic(String code, String start_date, String end_date) {
        JSONObject json = new JSONObject();
        JSONObject params = new JSONObject();
        params.put("ts_code", code);
        params.put("start_date", start_date);
        params.put("end_date", end_date);
        //接口名称
        json.put("api_name", "daily_basic");
        json.put("params", params);
        String result = post(json);
        JSONObject datas = JSON.parseObject(result);
        JSONArray items = datas.getJSONObject("data").getJSONArray("items");
        return items;
    }

    /**
     * https://tushare.pro/document/2?doc_id=128
     * @param date
     * @return
     */
    public JSONArray index_dailybasic(String date){
        JSONObject json = new JSONObject();
        JSONObject params = new JSONObject();
        params.put("trade_date", date);
        //接口名称
        json.put("api_name", "index_dailybasic");
        json.put("params", params);
        json.put("fields","ts_code,trade_date,turnover_rate,turnover_rate_f,pe,pe_ttm,pb");
        String result = post(json);
        JSONObject datas = JSON.parseObject(result);
        JSONArray items = datas.getJSONObject("data").getJSONArray("items");
        return items;
    }

    public static void main(String[] args) {
       /* try {
            //{"date":"2018-11-01 18:06:25","code":200,"address":"湖南省长沙市 电信","ip":"113.246.64.67"}
            System.setProperty("https.maxRedirects", "50");
            System.getProperties().setProperty("https.proxySet", "true");
            System.getProperties().setProperty("https.proxyHost", "116.209.55.238");
            System.getProperties().setProperty("https.proxyPort", "9999");
            StockSpider.enableSSLSocket();
            String json =Jsoup.connect("https://www.ipip.net/").get().text();
            System.out.println(json);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }*/
        TradingDateUtil tradingDateUtil = new TradingDateUtil();
//        String startDate = tradingDateUtil.minusDate(10, 0, 0, "yyyyMMdd");
        JSONArray data = new TushareApi().index_dailybasic("20200205");
        for (int i = 0; i < data.size(); i++) {
            JSONArray ja = data.getJSONArray(i);
            System.out.println(ja);
        }
    }
}
